package com.ruoyi.management.basics.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.management.basics.domain.TEmployeeMer;
import com.ruoyi.management.basics.mapper.TEmployeeMerMapper;
import com.ruoyi.management.basics.ro.DownloadTemplateParam;
import com.ruoyi.management.basics.ro.GetEmployeeMerListParam;
import com.ruoyi.management.basics.ro.ImportTemplateParam;
import com.ruoyi.management.basics.service.ITEmployeeMerService;
import com.ruoyi.management.basics.vo.DownloadTemplateVo;
import com.ruoyi.management.basics.vo.DriverMerListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 驾驶员商户信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-12-07
 */
@Service
public class TEmployeeMerServiceImpl implements ITEmployeeMerService
{
    @Autowired
    private TEmployeeMerMapper tEmployeeMerMapper;

    /**
     * 查询驾驶员商户信息
     * 
     * @param id 驾驶员商户信息主键
     * @return 驾驶员商户信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public TEmployeeMer selectTEmployeeMerById(String id)
    {
        return tEmployeeMerMapper.selectTEmployeeMerById(id);
    }

    /**
     * 查询驾驶员商户信息列表
     * 
     * @param tEmployeeMer 驾驶员商户信息
     * @return 驾驶员商户信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<TEmployeeMer> selectTEmployeeMerList(TEmployeeMer tEmployeeMer)
    {
        return tEmployeeMerMapper.selectTEmployeeMerList(tEmployeeMer);
    }

    /**
     * 新增驾驶员商户信息
     * 
     * @param tEmployeeMer 驾驶员商户信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertTEmployeeMer(TEmployeeMer tEmployeeMer)
    {
        tEmployeeMer.setCreateTime(DateUtils.getNowDate());

        String secreatKey = tEmployeeMer.getSecretkey();

        if (null!=secreatKey && secreatKey.length()>30)
        {
            String pub32tr2 = secreatKey.substring(secreatKey.length()-30);
            tEmployeeMer.setPub32tr2(pub32tr2);
        }

        return tEmployeeMerMapper.insertTEmployeeMer(tEmployeeMer);
    }

    /**
     * 修改驾驶员商户信息
     * 
     * @param tEmployeeMer 驾驶员商户信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateTEmployeeMer(TEmployeeMer tEmployeeMer)
    {
        tEmployeeMer.setUpdateTime(DateUtils.getNowDate());
        String secreatKey = tEmployeeMer.getSecretkey();

        if (null!=secreatKey && secreatKey.length()>30)
        {
            String pub32tr2 = secreatKey.substring(secreatKey.length()-30);
            tEmployeeMer.setPub32tr2(pub32tr2);
        }
        return tEmployeeMerMapper.updateTEmployeeMer(tEmployeeMer);
    }

    /**
     * 批量删除驾驶员商户信息
     * 
     * @param ids 需要删除的驾驶员商户信息主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteTEmployeeMerByIds(String[] ids)
    {
        return tEmployeeMerMapper.deleteTEmployeeMerByIds(ids);
    }

    /**
     * 删除驾驶员商户信息信息
     * 
     * @param id 驾驶员商户信息主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteTEmployeeMerById(String id)
    {
        return tEmployeeMerMapper.deleteTEmployeeMerById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<DriverMerListVo> selectTEmployeeMerVoList(GetEmployeeMerListParam tEmployeeMer) {
        return tEmployeeMerMapper.selectTEmployeeMerVoList(tEmployeeMer);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DriverMerListVo selectTEmployeeMerVoById(String id) {
        return tEmployeeMerMapper.selectTEmployeeMerVoById(id);
    }

    @Override
    @Transactional
    public List<DownloadTemplateVo> ImportTemplate(ImportTemplateParam param) {
        return tEmployeeMerMapper.ImportTemplate(param);
    }

    @Override
    @Transactional
    public List<DownloadTemplateVo> downloadTemplate(DownloadTemplateParam param) {
        return tEmployeeMerMapper.downloadTemplate(param.getEmployeeIdList());
    }

    @Override
    @Transactional
    public int importData(List<DownloadTemplateVo> paramList) {
        if(!CollectionUtils.isEmpty(paramList)){
            paramList = paramList.stream()
                    .filter(param-> param.getEmployeeId() != null
                            && !StringUtils.isEmpty(param.getMerchantid())
                            && !StringUtils.isEmpty(param.getPostid())
                            && !StringUtils.isEmpty(param.getBranchid())
                            && !StringUtils.isEmpty(param.getSecretkey()))
                    .collect(Collectors.toList());
        }
        if(CollectionUtils.isEmpty(paramList)){
            throw new ServiceException("导入数据不能为空！");
        }
        return tEmployeeMerMapper.importData(paramList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DriverMerListVo getDriverMerByEmployeeId(Long employeeId) {
        return tEmployeeMerMapper.selectDriverMerByEmployeeId(employeeId);
    }
}
